package com.scm.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.scm.model.Customer;
import com.scm.util.DbUtil;

public class CustomerDao {
	private Connection conn;
	
	public CustomerDao() {
		super();
	}

	public CustomerDao(Connection conn) {
		super();
		this.conn = conn;
	}
	
	public Customer login(String customercode,String password) throws SQLException {
		conn=null;
		PreparedStatement st = null;
		ResultSet rs = null;
		Customer customer = null;
		try {
			conn = DbUtil.getConn();
			st = conn.prepareStatement("select * from customer where customercode=? and password=?");
			st.setString(1, customercode);
			st.setString(2, password);
			rs = st.executeQuery();
			if(rs.next()) {
				customer = new Customer();
				customer.setPostcode(rs.getString("postcode"));
				customer.setName(rs.getString("name"));
				customer.setPassword(password);
				customer.setCustomercode(customercode);
				customer.setContactor(rs.getString("contactor"));
				customer.setAddress(rs.getString("address"));
				customer.setCreatedate(rs.getString("createdate"));
				customer.setFax(rs.getString("fax"));
				customer.setTel(rs.getString("tel"));
			}
		} finally {
			DbUtil.close(conn, rs, st);
		}
	return customer;
	}
}
